package day_7_4;

public class Main3 {
    /**
     * 108. 将有序数组转换为二叉搜索树
     * https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/description/
     */
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public TreeNode sortedArrayToBST(int[] nums) {
        return dfs(nums,0,nums.length - 1);
    }

    public TreeNode dfs(int[] nums,int left,int right) {
        if(left > right) {
            return null;
        }

        int mid = (right + left) / 2;

        TreeNode root = new TreeNode(nums[mid]);
        root.left = dfs(nums,left,mid - 1);
        root.right = dfs(nums,mid + 1,right);
        return root;
    }
}
